﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using System.DirectoryServices;
using System.IO;
using GOM_PLAYER.control_subtitle;
using System.Threading;
using System.Runtime.InteropServices;

namespace GOM_PLAYER
{
    public partial class main_window : DevExpress.XtraBars.Ribbon.RibbonForm
    {


        ///
        ///mrhit
        /// 
        ///dua khai báo SubtitleHelper truyen vao tham so là duong dan file xml sutitle
        public SubtitleHelper playSub;
        public short pitch = 0;
        /// 
        ///
        //-----------temp--------------------------
        int temp_play_list_button = 1;
        int temp_play_button = 1;
        int temp_sound_button = 1;
        public int temp_control_panel_button = 1;
        public int temp_preferences_barbutton = 1;
        public int temp_index_logo = new int();
        public bool temp_reset_preferences = false;
        public int index_song;
        //-----------------------------------------

        public double startPos = 0;
        public double endPos = 0;
        public bool set_AB_Repeat = false;
        public int time_backward = 10;
        public int time_forward = 10;
        public bool setMaintainSpeed = false;
        public string urlSaveCap = "E:\\";

        //--------------------------------------------------------------------------------------
        public List<shortcut_keyboard> list_shortcut_keyboard = new List<shortcut_keyboard>();
        public List<shortcut_mouse> list_shortcut_mouse = new List<shortcut_mouse>();
        public List<skin> list_skin = new List<skin>();
        public List<logo> list_logo = new List<logo>();
        public List<song> list_song = new List<song>();
        //--------------------------------------------------------------------------------------

        public string path_image_pause_button = "images\\PauseNormal.png";
        public string path_image_play_button = "images\\PlayNormal.png";
        public string path_image_volume_button = "images\\VolumeNormalBlue.png";
        public string path_image_volume_mute_button = "images\\VolumeNormalBlue_mute.png";

        public control_panel ctp=new control_panel();
        public Preferences pr=new Preferences();
        public process_xml prxml = new process_xml();

        public main_window()
        {
            InitializeComponent();
        }

            

        public void load_logo()
        {
            //------------------load logo---------------------------------
            List<logo> list_logo = prxml.load_logo_inserted();
            for (int i = 0; i < list_logo.Count; i++)
            {
                DataGridViewRow dr = new DataGridViewRow();

                DataGridViewCell dcell;
                dcell = new DataGridViewTextBoxCell();
                dcell.Value = list_logo[i].name_logo;
                dr.Cells.Add(dcell);

                dcell = new DataGridViewButtonCell();
                dcell.Value = "Delete";
                dr.Cells.Add(dcell);

                dcell = new DataGridViewButtonCell();
                dcell.Value = "open directory";
                dr.Cells.Add(dcell);

                pr.logo_dataGridView.Rows.Add(dr);
              
                
            }
            //Thread tr = new Thread(pr.show_logo2);
           // tr.Start(0);
            pr.logo_dataGridView.Rows[0].Selected = true;
            //------------------load logo---------------------------------ẹ
        }

        public void load_skin()
        {
            //-------------------load skin--------------------------------
            List<string> list_path_skin = prxml.load_skin_inserted();
            List<skin> list_skin = new List<skin>();
            for (int i = 0; i < list_path_skin.Count; i++)
            {
                skin sk = new skin();
                sk = prxml.load_skin(list_path_skin[i]);
                list_skin.Add(sk);
            }
            try
            {
                for (int i = 0; i < list_skin.Count; i++)
                {
                    DataGridViewRow row = new DataGridViewRow();
                    DataGridViewCell cell;

                    cell = new DataGridViewTextBoxCell();
                    cell.Value = list_skin[i].name_skin;
                    row.Cells.Add(cell);


                    cell = new DataGridViewTextBoxCell();
                    cell.Value = list_skin[i].file_name;
                    row.Cells.Add(cell);

                    cell = new DataGridViewButtonCell();
                    cell.Value = "Delete";
                    row.Cells.Add(cell);

                    cell = new DataGridViewButtonCell();
                    cell.Value = "Open directory";
                    row.Cells.Add(cell);

                    pr.skin_dataGridView.Rows.Add(row);
                }
                
            }
            catch (Exception)
            { }
            if (temp_reset_preferences == true)
                pr.skin_dataGridView.Rows[0].Selected = true;
            //------------------------load skin-------------------------------------------------
        }

        public void load_playlist_auto()
        {
            //load playlist autol
            list_song = prxml.load_playlist(Application.StartupPath + "\\playlist.xml");
            if (list_song.Count > 0)
            {
                playlist_listBox.DataSource = list_song;
                auto_save_playlist_barCheckItem.Checked = true;
            }
            else
            {
                auto_save_playlist_barCheckItem.Checked = false;
            }
        }

        public void init()
        {
          
            // init control panel and preferences
            ctp = new control_panel(this);
            pr = new Preferences(this);
            // this.axMoviePlayer.UseVMR9 = false;
            sound_trackBar.Value = 100;
            axMoviePlayer.ClearFullScreenType = 0;
            axMoviePlayer.ClearFullScreenKey = 32;
            // avoid refresh issue
            this.axMoviePlayer.RegisterMsg();
            //
            this.axMoviePlayer.ScrollTextPage = 1;
            // do not use microsoft mpeg2 decoder if you playback mpeg2 file
            this.axMoviePlayer.UseDecoderFilter = true;
            this.axMoviePlayer.DecoderFilterType = MOVIEPLAYERLib.MYDECODERFILTERTYPE.BLACKLIST;
            this.axMoviePlayer.DecoderAdd2BlackList("Microsoft DTV-DVD Video Decoder");
            this.axMoviePlayer.DecoderAdd2BlackList("Microsoft DTV-DVD Audio Decoder");
            //
            this.axMoviePlayer.UsePitchTempo = true;
            this.axMoviePlayer.UseVMR9 = true;
            //
            playlist_listBox.DataSource = new List<song>();

            //load 

            load_playlist_auto();

            list_shortcut_keyboard =prxml.load_shortcut_keyboard();
            pr.user_keyboard_defined_dataGridVew.DataSource =list_shortcut_keyboard;

            list_shortcut_mouse = prxml.load_shortcut_mouse();
            load_checked_user_mouse_defined();

            //
            change_status_not_openfile();
            //
            load_skin();

            load_logo();
            //
            axMoviePlayer.Refresh();
        }

        public void load_checked_user_mouse_defined()
        {
            for (int i = 0; i < list_shortcut_mouse.Count; i++)
            {
                if (list_shortcut_mouse[i].shortcut_key == "1")
                {
                    if (list_shortcut_mouse[i].description == "Default")
                    {
                        pr.left_single_Play_barCheckItem.Checked = false;
                        pr.left_single_Stop_barCheckItem.Checked = false;
                        pr.left_single_Pause_barCheckItem.Checked = false;
                        pr.left_single_Nextfile_barCheckItem.Checked = false;
                        pr.left_single_Previousfile_barCheckItem.Checked = false;
                        pr.left_single_default_barCheckItem.Checked = true;
                    }
                    if (list_shortcut_mouse[i].description == "Play")
                    {
                        pr.left_single_Play_barCheckItem.Checked = true;
                        pr.left_single_Stop_barCheckItem.Checked = false;
                        pr.left_single_Pause_barCheckItem.Checked = false;
                        pr.left_single_Nextfile_barCheckItem.Checked = false;
                        pr.left_single_Previousfile_barCheckItem.Checked = false;
                        pr.left_single_default_barCheckItem.Checked = false;
                    }
                    if (list_shortcut_mouse[i].description == "Pause")
                    {
                        pr.left_single_Play_barCheckItem.Checked = false;
                        pr.left_single_Stop_barCheckItem.Checked = false;
                        pr.left_single_Pause_barCheckItem.Checked = true;
                        pr.left_single_Nextfile_barCheckItem.Checked = false;
                        pr.left_single_Previousfile_barCheckItem.Checked = false;
                        pr.left_single_default_barCheckItem.Checked = false;
                    }

                    if (list_shortcut_mouse[i].description == "Stop")
                    {
                        pr.left_single_Play_barCheckItem.Checked = false;
                        pr.left_single_Stop_barCheckItem.Checked = true;
                        pr.left_single_Pause_barCheckItem.Checked = false;
                        pr.left_single_Nextfile_barCheckItem.Checked = false;
                        pr.left_single_Previousfile_barCheckItem.Checked = false;
                        pr.left_single_default_barCheckItem.Checked = false;
                    }

                    if (list_shortcut_mouse[i].description == "Next file")
                    {
                        pr.left_single_Play_barCheckItem.Checked = false;
                        pr.left_single_Stop_barCheckItem.Checked = false;
                        pr.left_single_Pause_barCheckItem.Checked = false;
                        pr.left_single_Nextfile_barCheckItem.Checked = true;
                        pr.left_single_Previousfile_barCheckItem.Checked = false;
                        pr.left_single_default_barCheckItem.Checked = false;
                    }

                    if (list_shortcut_mouse[i].description == "Previous file")
                    {
                        pr.left_single_Play_barCheckItem.Checked = false;
                        pr.left_single_Stop_barCheckItem.Checked = false;
                        pr.left_single_Pause_barCheckItem.Checked = false;
                        pr.left_single_Nextfile_barCheckItem.Checked = false;
                        pr.left_single_Previousfile_barCheckItem.Checked = true;
                        pr.left_single_default_barCheckItem.Checked = false;
                    }
                }
            }
        }

        public void GOM_Load(object sender, EventArgs e)
        {
            init();
            
        }

        private void Preferences_ItemClick(object sender, ItemClickEventArgs e)
        {
            Preferences pr = new Preferences(this);
            pr.Show();
        }

        private void open_file_ItemClick(object sender, ItemClickEventArgs e)
        {
            list_song = new List<song>();
            make_open_file();
            if (list_song.Count > 0)
            {
                axMoviePlayer.FileName = list_song[0].path_song;
                duration_textbox.Text = convert_to_time((int)axMoviePlayer.Duration);
                timeline_trackBar.Maximum = (int)axMoviePlayer.Duration;
                change_status_play();
                axMoviePlayer.Play();
            }
        }
        private void control_panel_button_Click(object sender, EventArgs e)
        {

            if ((temp_control_panel_button % 2) != 0)
            {

                ctp.Show();
                temp_control_panel_button++;
            }
            else
            {
                ctp.Hide();
                temp_control_panel_button++;

            }
        }

        private void play_Click(object sender, EventArgs e)
        {

            if ((temp_play_button % 2) != 0)
            {
                make_play();
               
            }
            else
            {

                make_pause();
            }
        }

        public void change_status_play()
        {

            play_button.Image = Bitmap.FromFile(path_image_pause_button);
            temp_play_button++;
            if (playlist_listBox.Items.Count == 1)
            {
                next_file_button.Enabled = false;
                previous_file_button.Enabled = false;
                this.stop_button.Enabled = true;
            }
            else
                if (list_song.Count > 1 && axMoviePlayer.FileName != list_song[list_song.Count - 1].path_song && axMoviePlayer.FileName != list_song[0].path_song)
                {
                    next_file_button.Enabled = true;
                    previous_file_button.Enabled = true;
                    this.stop_button.Enabled = true;
                }
                else
                    if (playlist_listBox.Items.Count > 1 && axMoviePlayer.FileName == list_song[0].path_song)
                    {
                        next_file_button.Enabled = true;
                        previous_file_button.Enabled = false;
                        this.stop_button.Enabled = true;
                    }
                    else
                        if (playlist_listBox.Items.Count > 1 && axMoviePlayer.FileName == list_song[playlist_listBox.Items.Count-1].path_song)
                        {
                            next_file_button.Enabled = false;
                            previous_file_button.Enabled = true;
                            this.stop_button.Enabled = true;
                        }
        }

        public void change_status_pause()
        {
            play_button.Image = Bitmap.FromFile(path_image_play_button);
            temp_play_button++;
            if (playlist_listBox.Items.Count == 1)
            {
                next_file_button.Enabled = false;
                previous_file_button.Enabled = false;
                this.stop_button.Enabled = true;
            }
            else
                if (playlist_listBox.Items.Count > 1 && axMoviePlayer.FileName != list_song[playlist_listBox.Items.Count - 1].path_song && axMoviePlayer.FileName != list_song[0].path_song)
                {
                    next_file_button.Enabled = true;
                    previous_file_button.Enabled = true;
                    this.stop_button.Enabled = true;
                }
                else
                    if (playlist_listBox.Items.Count > 1 && axMoviePlayer.FileName == list_song[0].path_song)
                    {
                        next_file_button.Enabled = true;
                        previous_file_button.Enabled = false;
                        this.stop_button.Enabled = true;
                    }
                    else
                        if (playlist_listBox.Items.Count > 1 && axMoviePlayer.FileName == list_song[playlist_listBox.Items.Count - 1].path_song)
                        {
                            next_file_button.Enabled = false;
                            previous_file_button.Enabled = true;
                            this.stop_button.Enabled = true;
                        }
        }

        public void change_status_stop()
        {
            if(playlist_listBox.Items.Count>0)
                index_song = playlist_listBox.SelectedIndices[0];
            temp_play_button = 1;
            play_button.Image = Bitmap.FromFile(path_image_play_button);
            timeline_trackBar.Value = 0;
            if (playlist_listBox.Items.Count == 1)
            {
                next_file_button.Enabled = false;
                previous_file_button.Enabled = false;
                this.stop_button.Enabled = false;
            }
            else
                if (playlist_listBox.Items.Count > 1 && axMoviePlayer.FileName != list_song[list_song.Count - 1].path_song && axMoviePlayer.FileName != list_song[0].path_song)
                {
                    next_file_button.Enabled = true;
                    previous_file_button.Enabled = true;
                    this.stop_button.Enabled = false;
                }
                else
                    if (playlist_listBox.Items.Count > 1 && axMoviePlayer.FileName == list_song[0].path_song)
                    {
                        next_file_button.Enabled = true;
                        previous_file_button.Enabled = false;
                        this.stop_button.Enabled = false;
                    }
                    else
                        if (playlist_listBox.Items.Count > 1 && axMoviePlayer.FileName == list_song[playlist_listBox.Items.Count - 1].path_song)
                        {
                            next_file_button.Enabled = false;
                            previous_file_button.Enabled = true;
                            this.stop_button.Enabled = false;
                        }
          
        }

        public void change_status_not_openfile()
        {
            this.stop_button.Enabled = false;
            next_file_button.Enabled = false;
            previous_file_button.Enabled = false;
            play_button.Image = Image.FromFile(path_image_play_button);
            timeline_trackBar.Value = 0;
            current_position_textbox.Text = "--:--:--";
            duration_textbox.Text = "--:--:--";
            axMoviePlayer.Stop();
            temp_play_button = 1;
            axMoviePlayer.FileName = "";
        }
        private void play_list_button_Click(object sender, EventArgs e)
        {
            if ((temp_play_list_button % 2) == 0)
            {
                splitContainerControl.PanelVisibility = SplitPanelVisibility.Both;
                temp_play_list_button++;
                axMoviePlayer.ResizeControl((short)axMoviePlayer.Width, (short)axMoviePlayer.Height);
                axMoviePlayer.Refresh();

            }
            else
            {
                splitContainerControl.PanelVisibility = SplitPanelVisibility.Panel1;
                temp_play_list_button++;
                axMoviePlayer.ResizeControl((short)axMoviePlayer.Width, (short)axMoviePlayer.Height);
                axMoviePlayer.Refresh();

            }
            if (!setMaintainSpeed && axMoviePlayer.FileName != "")
            {
                axMoviePlayer.PlaybackRate = 1;
                axMoviePlayer.Refresh();
            }
        }

        private void full_screen_Click(object sender, EventArgs e)
        {
            axMoviePlayer.ShowFullScreen(true);
        }

        private void next_file_button_Click(object sender, EventArgs e)
        {
            make_next_file();
           
        }
        private void previous_file_button_Click(object sender, EventArgs e)
        {
            make_previous_file();
            
        }

        public void make_open_file()
        {
            try
            {
                using (OpenFileDialog ofd = new OpenFileDialog())
                {

                    ofd.Multiselect = true;
                    ofd.Filter = "All Files (*.*)|*.*|AVCHD, TS(*.m2ts,ts)|*.m2ts;*.ts|Vob (*.vob)|*.vob|F4V (*.f4v)|*.f4v|FLV (*.flv)|*.flv|mpg (*.mpg)|*.mpg|mpeg (*.mpeg)|*.mpeg|wmv (*.wmv)|*.wmv|avi (*.avi)|*.avi|mov (*.mov)|*.mov|mp4 (*.mp4)|*.mp4|3gp (*.3gp)|*.3gp";
                    if (ofd.ShowDialog(this) == DialogResult.OK)
                    {
                        string[] temp_listfile = ofd.FileNames;
                        for (int j = 0; j < temp_listfile.Length; j++)
                            list_song.Add(new song(temp_listfile[j].Remove(0, temp_listfile[j].LastIndexOf("\\") + 1), temp_listfile[j]));
                        playlist_listBox.DataSource = new List<song>();
                        playlist_listBox.DataSource = list_song;
                    }
                    startPos = 0;
                    endPos = axMoviePlayer.Duration;

                    if (!setMaintainSpeed)
                        axMoviePlayer.PlaybackRate = 1;
                    set_AB_Repeat = false;
                    axMoviePlayer.Refresh();
                }
            }
            catch (Exception)
            {

            }
        }

        public void make_play()
        {
            if (axMoviePlayer.FileName != "" && axMoviePlayer.FileName != "screen\\black.flv")
            {
                this.duration_textbox.Text = convert_to_time((int)axMoviePlayer.Duration);
                timeline_trackBar.Maximum = (int)axMoviePlayer.Duration;
                axMoviePlayer.Play();
                change_status_play();
                Thread tr = new Thread(showMessage);
                tr.Start("Play");
                return;
            }
            else
                if (axMoviePlayer.FileName == "screen\\black.flv" && playlist_listBox.Items.Count > 0)
                {
                    axMoviePlayer.FileName = list_song[index_song].path_song;
                    this.duration_textbox.Text = convert_to_time((int)axMoviePlayer.Duration);
                    timeline_trackBar.Maximum = (int)axMoviePlayer.Duration;
                    axMoviePlayer.Play();
                    change_status_play();
                    Thread tr = new Thread(showMessage);
                    tr.Start("Play");
                    return;
                }
            else
                if ((axMoviePlayer.FileName == "screen\\black.flv" || axMoviePlayer.FileName == "")  && playlist_listBox.Items.Count==0)
                {
                    make_open_file();
                    if (list_song.Count > 0)
                    {
                        this.axMoviePlayer.FileName = list_song[0].path_song;
                        this.duration_textbox.Text = convert_to_time((int)axMoviePlayer.Duration);
                        timeline_trackBar.Maximum = (int)axMoviePlayer.Duration;
                        axMoviePlayer.Play();
                        change_status_play();
                        Thread tr = new Thread(showMessage);
                        tr.Start(list_song[playlist_listBox.SelectedIndices[0]].name_song + " " + playlist_listBox.SelectedIndices[0] + "/" + playlist_listBox.Items.Count);
                    }
                }
          
            
        }

        public void make_pause()
        {
            axMoviePlayer.Pause();
            change_status_pause();
        }

        public void make_stop()
        {
            axMoviePlayer.Stop();
            change_status_stop();
        }

        protected void make_next_file()
        {
            if (shuffle_checkbutton.Checked == false)
            {
                for (int i = 0; i < list_song.Count; i++)
                    if (list_song[i].path_song == axMoviePlayer.FileName)
                    {
                        axMoviePlayer.FileName = list_song[i + 1].path_song;
                        playlist_listBox.ClearSelected();
                        playlist_listBox.SetSelected(i + 1, true);
                        this.timeline_trackBar.Maximum = (int)this.axMoviePlayer.Duration;
                        this.duration_textbox.Text = convert_to_time((int)axMoviePlayer.Duration);
                        change_status_play();
                        if (!setMaintainSpeed)
                            axMoviePlayer.PlaybackRate = 1;
                        axMoviePlayer.Refresh();
                        axMoviePlayer.Play();
                        set_AB_Repeat = false;
                        axMoviePlayer.ChangePitch(this.pitch);
                        break;
                    }
            }
            else
            {
                int i = random_song();
                axMoviePlayer.FileName = list_song[i].path_song;
                playlist_listBox.ClearSelected();
                playlist_listBox.SetSelected(i, true);
                this.timeline_trackBar.Maximum = (int)this.axMoviePlayer.Duration;
                this.duration_textbox.Text = convert_to_time((int)axMoviePlayer.Duration);
                change_status_play();
                if (!setMaintainSpeed)
                    axMoviePlayer.PlaybackRate = 1;
                axMoviePlayer.Refresh();
                axMoviePlayer.Play();
                set_AB_Repeat = false;
                axMoviePlayer.Refresh();
                axMoviePlayer.ChangePitch(this.pitch); 
            }
            Thread tr = new Thread(showMessage);
            tr.Start(list_song[playlist_listBox.SelectedIndices[0]].name_song + " " + playlist_listBox.SelectedIndices[0] + "/" + playlist_listBox.Items.Count);
        }

        protected void make_previous_file()
        {
            if (shuffle_checkbutton.Checked == false)
            {
                for (int i = 0; i < playlist_listBox.Items.Count; i++)
                    if (list_song[i].path_song == axMoviePlayer.FileName)
                    {
                        axMoviePlayer.FileName = list_song[i - 1].path_song;
                        playlist_listBox.ClearSelected();
                        playlist_listBox.SetSelected(i - 1, true);
                        this.timeline_trackBar.Maximum = (int)this.axMoviePlayer.Duration;
                        this.duration_textbox.Text = convert_to_time((int)axMoviePlayer.Duration);
                        change_status_play();
                        if (!setMaintainSpeed)
                            axMoviePlayer.PlaybackRate = 1;
                        axMoviePlayer.Refresh();
                        axMoviePlayer.Play();
                        set_AB_Repeat = false;
                        axMoviePlayer.ChangePitch(this.pitch);
                        break;
                    }
            }
            else
            {
                int i = random_song();
                axMoviePlayer.FileName = list_song[i].path_song;
                playlist_listBox.ClearSelected();
                playlist_listBox.SetSelected(i, true);
                this.timeline_trackBar.Maximum = (int)this.axMoviePlayer.Duration;
                this.duration_textbox.Text = convert_to_time((int)axMoviePlayer.Duration);
                change_status_play();
                if (!setMaintainSpeed)
                    axMoviePlayer.PlaybackRate = 1;
                axMoviePlayer.Refresh();
                axMoviePlayer.Play();
                set_AB_Repeat = false;
                axMoviePlayer.ChangePitch(this.pitch);
            }
            Thread tr = new Thread(showMessage);
            tr.Start(list_song[playlist_listBox.SelectedIndices[0]].name_song + " " + playlist_listBox.SelectedIndices[0] + "/" + playlist_listBox.Items.Count);
        }

        protected void make_repeat()
        {
            if (shuffle_checkbutton.Checked == false)
            {
                if (axMoviePlayer.FileName == list_song[playlist_listBox.Items.Count - 1].path_song)
                {
                    axMoviePlayer.FileName = list_song[0].path_song;
                    playlist_listBox.ClearSelected();
                    playlist_listBox.SetSelected(0, true);
                    this.timeline_trackBar.Maximum = (int)this.axMoviePlayer.Duration;
                    this.duration_textbox.Text = convert_to_time((int)axMoviePlayer.Duration);
                    change_status_play();
                    if (!setMaintainSpeed)
                        axMoviePlayer.PlaybackRate = 1;
                    axMoviePlayer.Refresh();
                    axMoviePlayer.Play();
                    set_AB_Repeat = false;
                    axMoviePlayer.ChangePitch(this.pitch);
                }
            }
            else
            {
                int i = random_song();
                axMoviePlayer.FileName = list_song[i].path_song;
                playlist_listBox.ClearSelected();
                playlist_listBox.SetSelected(i, true);
                this.timeline_trackBar.Maximum = (int)this.axMoviePlayer.Duration;
                this.duration_textbox.Text = convert_to_time((int)axMoviePlayer.Duration);
                change_status_play();
                if (!setMaintainSpeed)
                    axMoviePlayer.PlaybackRate = 1;
                axMoviePlayer.Refresh();
                axMoviePlayer.Play();
                set_AB_Repeat = false;
                axMoviePlayer.ChangePitch(this.pitch);
            }
            Thread tr = new Thread(showMessage);
            tr.Start(list_song[playlist_listBox.SelectedIndices[0]].name_song + " " + playlist_listBox.SelectedIndices[0] + "/" + playlist_listBox.Items.Count);
        }

        private void axMoviePlayer_OnCompleted(object sender, EventArgs e)
        {

            if (set_AB_Repeat == true && endPos == axMoviePlayer.Duration)
            {
                axMoviePlayer.SetPos(startPos);
                axMoviePlayer.Play();
                return;
            }
            else
            {
                //this.trackBar_timeline.Value = Math.Min((int)this.axMoviePlayer.Duration, this.trackBar_timeline.Maximum);
                if (this.repeat_checkbutton.Checked == true && axMoviePlayer.FileName == list_song[playlist_listBox.Items.Count - 1].path_song)
                {
                    make_repeat();
                    return;
                }
                else
                    if (repeat_checkbutton.Checked == false && shuffle_checkbutton.Checked == false && axMoviePlayer.FileName == list_song[playlist_listBox.Items.Count - 1].path_song)
                    {
                        change_status_pause();
                        return;
                    }
                    else
                        if (list_song.Count > 1)
                        {
                            make_next_file();
                            return;
                        }
                        else
                            if(list_song.Count==1)
                            {
                                change_status_pause();
                            }
            }
        }

        private void axMoviePlayer_OnPlaying(object sender, AxMOVIEPLAYERLib._DMoviePlayerEvents_OnPlayingEvent e)
        {
            if (stop_button.Enabled == true)
            {
                this.timeline_trackBar.Value = (int)e.iCurrent;
                this.current_position_textbox.Text = convert_to_time((int)e.iCurrent);
                
            }
            if (!set_AB_Repeat)
            {
                startPos = 0;
                endPos = axMoviePlayer.Duration;
                ctp.time_repeat_A_textBox.Text = "00:00:00";
                ctp.time_repeat_B_textBox.Text = "00:00:00";
            }
            if ((e.iCurrent >= endPos || e.iCurrent < startPos) && set_AB_Repeat)
            {
                axMoviePlayer.SetPos(startPos);
            }

            if (playSub != null && Show_subtitle_barCheckItem.Checked == true)
            {
                playSub.onPlaying((int)e.iCurrent, axMoviePlayer);
                ctp.size_sub_trackBarControl.Enabled = true;
                ctp.x_sub_trackBarControl.Enabled = true;
                ctp.y_sub_trackBarControl.Enabled = true;
                ctp.faster0_5_simpleButton.Enabled = true;
                ctp.slower_0_5_simpleButton.Enabled = true;
                ctp.reset_size_sub_simpleButton1.Enabled = true;
                ctp.reset_x_simpleButton.Enabled = true;
                ctp.reset_y_simpleButton.Enabled = true;
            }
            else
            {
                ctp.size_sub_trackBarControl.Enabled = false;
                ctp.x_sub_trackBarControl.Enabled = false;
                ctp.y_sub_trackBarControl.Enabled = false;
                ctp.faster0_5_simpleButton.Enabled = false;
                ctp.slower_0_5_simpleButton.Enabled = false;
                ctp.reset_size_sub_simpleButton1.Enabled = false;
                ctp.reset_x_simpleButton.Enabled = false;
                ctp.reset_y_simpleButton.Enabled = false; 
            }

            //if (playlist_listBox.Items.Count > 0)
            //{
            //    del_dropDownButton.Enabled = true;
            //    sel_dropDownButton.Enabled = true;
            //    sort_dropDownButton.Enabled = true;
            //    list_dropDownButton.Enabled = true;
            //    shuffle_checkbutton.Enabled = true;
            //    repeat_checkbutton.Enabled = true;
            //}
            //else
            //{
            //    del_dropDownButton.Enabled = false;
            //    sel_dropDownButton.Enabled = false;
            //    sort_dropDownButton.Enabled = false;
            //    list_dropDownButton.Enabled = false;
            //    shuffle_checkbutton.Enabled = false;
            //    repeat_checkbutton.Enabled = false;
            //}

        }

        private void stop_button_Click(object sender, EventArgs e)
        {
            change_status_stop();
            Thread tr = new Thread(pr.show_logo2);
            tr.Start(temp_index_logo);
            axMoviePlayer.Stop();

        }

        public string convert_to_time(int s)
        {
            string time = "--:--:--";
            int hh, mm, ss;
            if ((s % 3600) == 0)
            {
                hh = s / 3600;
                time = hh.ToString();
            }

            if ((s % 3600) != 0)
            {
                hh = s / 3600;
                mm = (s - (hh * 3600)) / 60;
                ss = (s - (mm * 60));
                //time = hh.ToString() + ":" + mm.ToString() + ":" + ss.ToString();
                time = (hh / 10).ToString() + (hh % 10).ToString() + ":" + (mm / 10).ToString() + (mm % 10).ToString() + ":" + (ss / 10).ToString() + (ss % 10).ToString();
            }
            return time;
        }



        private void playlist_listBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            axMoviePlayer.FileName = playlist_listBox.SelectedValue.ToString();
            this.timeline_trackBar.Maximum = (int)this.axMoviePlayer.Duration;
            duration_textbox.Text = convert_to_time((int)this.axMoviePlayer.Duration);
            change_status_play();
            if (!setMaintainSpeed)
                axMoviePlayer.PlaybackRate = 1;
            axMoviePlayer.Refresh();
            set_AB_Repeat = false;
            axMoviePlayer.Play();
            startPos = 0;
            endPos = axMoviePlayer.Duration;
            axMoviePlayer.ChangePitch(this.pitch);

        }



        private void timeline_trackBar_Scroll(object sender, EventArgs e)
        {
            axMoviePlayer.SetPos(timeline_trackBar.Value);
            axMoviePlayer.Refresh();

        }

        private void sound_trackBar_Scroll(object sender, EventArgs e)
        {
            adjustment_sound();
            Thread tr = new Thread(showMessage);
            tr.Start("Volume: " + ((sound_trackBar.Value * 100) / sound_trackBar.Maximum) + "%"); axMoviePlayer.Refresh();
        }

        private void sound_button_Click(object sender, EventArgs e)
        {
            if ((temp_sound_button % 2) != 0)
            {
                temp_sound_button++;
                axMoviePlayer.SoundVolume = -10000;
                volume_button.Image = Bitmap.FromFile(path_image_volume_mute_button);
                Thread tr = new Thread(showMessage);
                tr.Start("Disable sound");

            }
            else
            {
                temp_sound_button++;
                adjustment_sound();
                volume_button.Image = Bitmap.FromFile(path_image_volume_button);
                Thread tr = new Thread(showMessage);
                tr.Start("Enable sound");
            }
        }

        protected void adjustment_sound()
        {
            if (sound_trackBar.Value < 50 && sound_trackBar.Value > 0)
                axMoviePlayer.SoundVolume = sound_trackBar.Value - 1000;
            if (sound_trackBar.Value > 50 && sound_trackBar.Value < 100)
                axMoviePlayer.SoundVolume = sound_trackBar.Value - 100;
            if (sound_trackBar.Value == 0)
                axMoviePlayer.SoundVolume = -10000;
            if (sound_trackBar.Value == 100)
                axMoviePlayer.SoundVolume = 0;
        }

        protected int random_song()
        {
            Random rd = new Random();
            int i = rd.Next(0, playlist_listBox.Items.Count);
            return i;
        }

        private void opendirectory_barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {

            using (FolderBrowserDialog fbd = new FolderBrowserDialog())
            {
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    List<song> list_song2 = getfile(fbd.SelectedPath);
                    for (int i = 0; i < list_song2.Count; i++)
                        list_song.Add(list_song2[i]);
                    playlist_listBox.DataSource = new List<song>();
                    playlist_listBox.DataSource = list_song;
                }

            }
        }

        protected List<song> getfile(string path)
        {
            List<song> listfile = new List<song>();
            DirectoryInfo dir = new DirectoryInfo(path);
            string[] strfilter = { "*.m2ts", "*.ts", "*.vob", "*.f4v", "*.flv", "*.mpg", "*.mpeg", "*.wmv", "*.avi", "*.mov", "*.mp4", "*.3gp" };
            for (int i = 0; i < strfilter.Length; i++)
                foreach (FileInfo fi in dir.GetFiles(strfilter[i]))
                    listfile.Add(new song(fi.FullName.Remove(0, fi.FullName.LastIndexOf("\\") + 1), fi.FullName));
            return listfile;
        }

        private void del_selectfile_barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {

            for (int i = 0; i < playlist_listBox.SelectedItems.Count; i++)
                list_song.RemoveAt(playlist_listBox.SelectedIndices[i]);
            playlist_listBox.DataSource = new List<song>();
            playlist_listBox.DataSource = list_song;
        }

        private void del_deselectfile_barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            List<song> list_song2 = new List<song>();
            for (int i = 0; i < playlist_listBox.SelectedItems.Count; i++)
                list_song2.Add(new song(list_song[playlist_listBox.SelectedIndices[i]].name_song, list_song[playlist_listBox.SelectedIndices[i]].path_song));
            list_song = list_song2;
            playlist_listBox.DataSource = new List<song>();
            playlist_listBox.DataSource = list_song;
        }

        private void del_all_barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            list_song.Clear();
            playlist_listBox.DataSource = new List<song>();
        }

        private void selectall_barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {

            for (int i = 0; i < playlist_listBox.Items.Count; i++)
                playlist_listBox.SetSelected(i, true);
        }

        private void open_file_barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {

            make_open_file();
        }


        private void open_preferences_barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {

            if ((temp_preferences_barbutton % 2) != 0)
            {
                pr.Show();
                temp_preferences_barbutton++;
            }
            else
            {
                pr.Hide();
                temp_preferences_barbutton++;
            }
        }

        private void splitContainerControl_SplitterMoved(object sender, EventArgs e)
        {
            axMoviePlayer.ResizeControl((short)axMoviePlayer.Width, (short)axMoviePlayer.Height);
            axMoviePlayer.Refresh();
        }

        private void main_window_Resize(object sender, EventArgs e)
        {
            axMoviePlayer.ResizeControl((short)axMoviePlayer.Width, (short)axMoviePlayer.Height);
            axMoviePlayer.Refresh();
        }

        private void inverse_selection_barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            int[] item_selected = new int[playlist_listBox.SelectedItems.Count];
            for (int n = 0; n < playlist_listBox.SelectedItems.Count; n++)
                item_selected[n] = playlist_listBox.SelectedIndices[n];
            for (int i = 0; i < playlist_listBox.Items.Count; i++)
                playlist_listBox.SetSelected(i, true);
            for (int m = 0; m < item_selected.Length; m++)
                playlist_listBox.SetSelected(item_selected[m], false);
        }

        [DllImport("user32.dll")]
        private static extern short GetAsyncKeyState(Keys vKey);

        private void main_window_KeyDown(object sender, KeyEventArgs e)
        {
            for (int i = 0; i < list_shortcut_keyboard.Count; i++)
            {
                string strkey = "";
                if (e.Control == false && e.Alt == false && e.Shift == false)
                {

                    strkey = e.KeyCode.ToString();
                }
                if (e.Control == true && e.Alt == false && e.Shift == false)
                {
                    strkey = "CTRL + " + e.KeyCode.ToString();
                }
                if (e.Control == false && e.Alt == true && e.Shift == false)
                {
                    strkey = "ALT + " + e.KeyCode.ToString();
                }
                if (e.Control == false && e.Alt == false && e.Shift == true)
                {
                    strkey = "SHIFT + " + e.KeyCode.ToString();
                }
                if (e.Control && e.Alt && e.Shift == false)
                {
                    strkey = "CTRL + ALT + " + e.KeyCode.ToString();
                }
                if (e.Control == false && e.Alt && e.Shift)
                {
                    strkey = "SHIFT + ALT + " + e.KeyCode.ToString();
                }
                if (e.Control && e.Alt == false && e.Shift)
                {
                    strkey = "SHIFT + CTRL + " + e.KeyCode.ToString();
                }
                if (list_shortcut_keyboard[i].description == "Play" && strkey == list_shortcut_keyboard[i].shortcut_key)
                {
                    change_status_play();
                    axMoviePlayer.Play();
                }

                if (list_shortcut_keyboard[i].description == "Stop" && strkey == list_shortcut_keyboard[i].shortcut_key)
                {
                    change_status_stop();
                    axMoviePlayer.Stop();
                }

                if (list_shortcut_keyboard[i].description == "Pause" && strkey == list_shortcut_keyboard[i].shortcut_key)
                {
                    change_status_pause();
                    axMoviePlayer.Pause();
                }

                if (list_shortcut_keyboard[i].description == "Next file" && strkey == list_shortcut_keyboard[i].shortcut_key)
                {
                    make_next_file();
                }

                if (list_shortcut_keyboard[i].description == "Previous file" && strkey == list_shortcut_keyboard[i].shortcut_key)
                {
                    make_previous_file();
                }
            }
        }

        private void axMoviePlayer_MouseDownEvent(object sender, AxMOVIEPLAYERLib._DMoviePlayerEvents_MouseDownEvent e)
        {
            for (int i = 0; i < list_shortcut_mouse.Count; i++)
            {
                if (e.button.ToString() == list_shortcut_mouse[i].shortcut_key && list_shortcut_mouse[i].description == "Play")
                {
                    //change_status_play();
                    //axMoviePlayer.Play(); 
                    make_play();
                    Thread tr = new Thread(showMessage);
                    tr.Start("Play");
                }
                else
                    if (e.button.ToString() == list_shortcut_mouse[i].shortcut_key && list_shortcut_mouse[i].description == "Pause")
                    {
                        change_status_pause();
                        axMoviePlayer.Pause();
                    }
                    else
                        if (e.button.ToString() == list_shortcut_mouse[i].shortcut_key && list_shortcut_mouse[i].description == "Stop")
                        {
                            change_status_stop();
                            axMoviePlayer.Stop();
                        }
                        else
                            if (e.button.ToString() == list_shortcut_mouse[i].shortcut_key && list_shortcut_mouse[i].description == "Next file")
                            {
                                make_next_file();
                                Thread tr = new Thread(showMessage);
                                tr.Start("Next file");
                            }
                            else
                                if (e.button.ToString() == list_shortcut_mouse[i].shortcut_key && list_shortcut_mouse[i].description == "Previous file")
                                {
                                    make_previous_file();
                                    Thread tr = new Thread(showMessage);
                                    tr.Start("Previous file");
                                }
            }
        }

        private void Open_subtitle_barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {

            using (OpenFileDialog ofd = new OpenFileDialog())
            {

                ofd.Filter = "All Files (*.*)|*.*|XML (*.xml)|*.xml";
                if (ofd.ShowDialog(this) == DialogResult.OK)
                {
                    playSub = new SubtitleHelper(ofd.FileName);
                    Thread tr = new Thread(showMessage);
                    string s = "Open subtitle";
                    tr.Start(s);
                }
            }
        }
        public void showMessage(object message)
        {
            axMoviePlayer.DrawText(0.0f, 0.0f, 0, "Recent Function: " + message.ToString(), 11, "arial", true, true, false, SubtitleHelper.Color2Uint32(Color.FromArgb(255, 128, 0))
                , SubtitleHelper.Color2Uint32(Color.FromArgb(0, 0, 0)), SubtitleHelper.Color2Uint32(Color.FromArgb(0, 0, 0)), 1, 500, 0);
            axMoviePlayer.Refresh();
            Thread.Sleep(15000);
            axMoviePlayer.DrawText(0.0f, 0.0f, 0, "", 1, "arial", true, true, false, SubtitleHelper.Color2Uint32(Color.FromArgb(255, 255, 255))
                , SubtitleHelper.Color2Uint32(Color.FromArgb(0, 0, 0)), SubtitleHelper.Color2Uint32(Color.FromArgb(0, 0, 0)), 1, 500, 0);
            axMoviePlayer.Refresh();
        }

        private void repeat_checkbutton_CheckedChanged(object sender, EventArgs e)
        {
            if (repeat_checkbutton.Checked==true)
            {
                Thread tr = new Thread(showMessage);
                tr.Start("Repeat on");
            }
            else
            {
                Thread tr = new Thread(showMessage);
                tr.Start("Repeat off");
            }
        }

        private void shuffle_checkbutton_CheckedChanged(object sender, EventArgs e)
        {
            if (shuffle_checkbutton.Checked==true)
            {
                Thread tr = new Thread(showMessage);
                tr.Start("Shuffle on");
            }
            else
            {
                Thread tr = new Thread(showMessage);
                tr.Start("Shuffle off");
            }
        }

        public void reset()
        {
            pr.Dispose();
            pr = new Preferences(this);
            pr.Show();
            temp_preferences_barbutton++;
        }

        private void playlist_listBox_DoubleClick(object sender, EventArgs e)
        {
            axMoviePlayer.FileName = playlist_listBox.SelectedValue.ToString();
            this.timeline_trackBar.Maximum = (int)this.axMoviePlayer.Duration;
            duration_textbox.Text = convert_to_time((int)this.axMoviePlayer.Duration);
            change_status_play();
            if (!setMaintainSpeed && playlist_listBox.Items.Count > 0)
                axMoviePlayer.PlaybackRate = 1;
            axMoviePlayer.Refresh();
            set_AB_Repeat = false;
            axMoviePlayer.Play();
            startPos = 0;
            endPos = axMoviePlayer.Duration;
            axMoviePlayer.ChangePitch(this.pitch);
        }

        private void playlist_listBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            axMoviePlayer.FileName = playlist_listBox.SelectedValue.ToString();
            change_status_play();
            axMoviePlayer.Play();
        }

        private void asc_barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            playlist_listBox.DataSource = new List<song>();
            playlist_listBox.Sorted = true;
            playlist_listBox.DataSource = list_song;
            list_song = new List<song>();
            list_song = (List<song>)playlist_listBox.DataSource;


        }

        private void desc_barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            //List<string> list_song_name = new List<string>();
            //List<string> list_song_path = new List<string>();
            //for (int i = 0; i < list_song.Count; i++)
            //{
            //    list_song_name.Add(list_song[i].name_song);
            //    list_song_path.Add(list_song[i].path_song);
            //}
            //    list_song.Reverse();
            //playlist_listBox.DataSource = new List<song>();
            //playlist_listBox.DataSource = list_song;
        }

        private void save_playlist_barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            SaveFileDialog sfd;
            using (sfd = new SaveFileDialog())
            {
                sfd.Filter = "XML (*.xml)|*.xml";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    File.Create(sfd.FileName).Dispose();
                    prxml.save_playlist(list_song, sfd.FileName);
                }

            }

        }

        private void open_playlist_barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenFileDialog ofd;
            using (ofd = new OpenFileDialog())
            {
                ofd.Filter = "XML (*.xml)|*.xml";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    list_song = prxml.load_playlist(ofd.FileName);
                    playlist_listBox.DataSource = new List<song>();
                    playlist_listBox.DataSource = list_song;
                }
            }
        }

        private void auto_save_playlist_barCheckItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (auto_save_playlist_barCheckItem.Checked == true)
            {
                File.Create(Application.StartupPath+"\\playlist.xml").Dispose();
                prxml.save_playlist(list_song, Application.StartupPath + "\\playlist.xml");
            }
            else
            {
                File.Delete(Application.StartupPath + "\\playlist.xml");
            }
        }

        private void Show_subtitle_barCheckItem_ItemClick(object sender, ItemClickEventArgs e)
        {
        }

        private void Show_subtitle_barCheckItem_CheckedChanged(object sender, ItemClickEventArgs e)
        {
            if (Show_subtitle_barCheckItem.Checked == true)
            {
                Thread tr = new Thread(showMessage);
                tr.Start("Show subtitles");
            }
            else
            {
                Thread tr = new Thread(showMessage);
                tr.Start("Hide subtitles");
            }
        }

    
    }

}